package quizman.mappers;
import java.sql.ResultSet;

import quizman.domain.Question;
import quizman.tdg.NumericAnswerTDG;

/**
 * 
 */

/**
 * @author Team0i
 *
 */
public class NumericAnswerMapper {
	public static double find(String questionName, int upperLower) throws Exception {
		try {
			ResultSet rs = NumericAnswerTDG.find(questionName);
			if(!rs.next()) return Double.NaN; 
			boolean isRight = (rs.getString("isRight").contains("TRUE"));
			double bound = Double.parseDouble(rs.getString("Answer_Text"));
			if((isRight && upperLower == 1) || (!isRight && upperLower == 2)) return bound;
			if(!rs.next()) return Double.NaN;
			isRight = (rs.getString("isRight").contains("TRUE"));
			bound = Double.parseDouble(rs.getString("Answer_Text"));
			if((isRight && upperLower == 1) || (!isRight && upperLower == 2)) return bound;
			return Double.NaN;
		} catch (Exception e) {
			String msg = "Error in NumericAnswer.find";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(Question q, double lowerBound, double upperBound) throws Exception {
		try {
			NumericAnswerTDG.insert(q.getName(), lowerBound, upperBound);
		} catch (Exception e) {
			String msg = "Error in NumericAnswer.insert";
			throw new Exception(msg,e);
		}
	}
	
	public static void update(Question q, double lowerBound, double upperBound) throws Exception {
		try {
			NumericAnswerTDG.update(q.getName(), lowerBound, upperBound);
		} catch (Exception e) {
			String msg = "Error in NumericAnswer.update";
			throw new Exception(msg,e);
		}
	}
	
	public static void delete(Question q) throws Exception {
		try {
			NumericAnswerTDG.delete(q.getName());
		} catch (Exception e) {
			String msg = "Error in NumericAnswer.delete";
			throw new Exception(msg,e);
		}
	}
}
